package com.think.common.utils;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;

/**
 * <p>
 * 响应信息主体
 * </p>
 *
 * @author: xwj
 * @data: 2019-04-21
 */
@Getter
@ToString
@Accessors(chain = true)
@AllArgsConstructor
public class ResultUtil<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private HttpStatus code;

    private T data;

    private String msg;

    public ResultUtil(HttpStatus code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultUtil(String msg) {
        this.msg = msg;
    }

    public static HttpEntity requestParamError() {
        HttpStatus status = HttpStatus.BAD_REQUEST;
        return new ResponseEntity(new ResultUtil(status, null, "请求失败，必要参数为空"), status);
    }

    public static HttpEntity createError() {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return new ResponseEntity(new ResultUtil(status, null, "新增失败"), status);
    }

    public static HttpEntity updateError() {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return new ResponseEntity(new ResultUtil(status, null, "更新失败"), status);
    }

    public static HttpEntity deleteError() {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return new ResponseEntity(new ResultUtil(status, null, "删除失败"), status);
    }

    public static HttpEntity getError() {
        HttpStatus status = HttpStatus.GONE;
        return new ResponseEntity(new ResultUtil(status, null, "资源不存在"), status);
    }

    public static HttpEntity error(String msg) {
        HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
        return new ResponseEntity(new ResultUtil(status, null, msg), status);
    }

    public static HttpEntity error(HttpStatus status, String msg) {
        return new ResponseEntity(new ResultUtil(status, null, msg), status);
    }

    public static HttpEntity success() {
        HttpStatus status = HttpStatus.OK;
        return new ResponseEntity(new ResultUtil(status, null, "success"), status);
    }

    public static HttpEntity createSuccess() {
        HttpStatus status = HttpStatus.CREATED;
        return new ResponseEntity(new ResultUtil(status, null, "success"), status);
    }

    public static HttpEntity deleteSuccess() {
        HttpStatus status = HttpStatus.NO_CONTENT;
        return new ResponseEntity(new ResultUtil(status, null, "success"), status);
    }

    public static HttpEntity success(HttpStatus status) {
        return new ResponseEntity(new ResultUtil(status, null, "success"), status);
    }

    public static HttpEntity success(Object data) {
        HttpStatus status = HttpStatus.OK;
        return new ResponseEntity(new ResultUtil(status, data, "success"), status);
    }

    public static HttpEntity error(Object data,String msg) {
        HttpStatus status = HttpStatus.NOT_EXTENDED;
        return new ResponseEntity(new ResultUtil(status, data, msg), status);
    }

    public static HttpEntity success(Object data, HttpStatus status) {
        return new ResponseEntity(new ResultUtil(status, data, "success"), status);
    }
}
